home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: uu4news.netcom.com!zodiac!szh
- From: szh@zcon.com (Syed Zaeem Hosain)
- Subject: Re: A Very Simple Socket Question
- Message-ID: <1996Jan16.212537.2710@zcon.com>
- Sender: szh@zcon.com (Syed Zaeem Hosain)
- Nntp-Posting-Host: zodiac
- Reply-To: szh@zcon.com
- Organization: Z Consulting Group
- References: <1996Jan16.174631.1892@zcon.com>
- Date: Tue, 16 Jan 1996 21:25:37 GMT
-
- In article <1996Jan16.174631.1892@zcon.com>, szh@zcon.com (Syed Zaeem Hosain) writes:
- >In article <4dfgj8$6p9@nntp.ucs.ubc.ca>, evil@unixg.ubc.ca (Peter Pan) writes:
- >>Please help! I am a C beginner.
- >>
- >>======================================
- >>Client:
- >>
- >>int sd, addrlen;
- >>struct sockaddr_in svaddr;
- >>
- >>char *data;
- >>data = "abcdef";
- >>
- >>..... /* skip */
- >>
- >>sendto(sd, data, strlen(data), 0, &svaddr, &addrlen);
- >>
- >>
- >>==========================================
- >>Server:
- >>
- >>char data;
- >>data = (char*) malloc( 100*sizeof(char) );
- >>
- >>recvfrom(sd2, data, sizeof(data), 0, &claddr, &addrlen);
- >>
- >>==========================================
- >>
- >>Output:
- >>
- >>data sent: abcdef
- >>data received: abcd
- >>
- >>==========================================
- >>
- >>Anyone knows why the data are different after transmittion.
- >
- >Well, I do not know if this is the problem (it may be a typo in your
- >post), but you might try declaring:
- >
- >char *data;
- >
- >on the server, rather than:
- >
- >char data;
- >
- >When you then assign "data = (char *) mall...", the very explicit cast
- >*probably* (but I am not sure) causes the error messages about this
- >mistake to be suppressed.
- >
- >Of course, there may still be something more wrong with the socket
- >transmission. But this is one place to get started.
-
- Ah! Upon a re-read on the server, I also see that the buffer size has
- been sent as "sizeof(data)". This is merely the size of a char pointer
- on your system (assuming that it is "char *data;" that you actually
- have in the code).
-
- So you need to change this to 100 * size of char, although sizeof(char)
- is 1, so you could simply use:
-
- recvfrom(sd2, data, 100, 0, &claddr, &addrlen);
-
- since you explicitly use 100 in the call to malloc rather than a macro
- definition or variable.
-
- Z
-
-
- --
- -------------------------------------------------------------------------
- | Syed Zaeem Hosain P. O. Box 610097 (408) 441-7021 |
- | Z Consulting Group San Jose, CA 95161 szh@zcon.com |
- -------------------------------------------------------------------------
-